home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Demos / A.D. Software / OOFILE / Buildable, limited OOFILE / samples / ooftst01.cpp next >
C/C++ Source or Header  |  1996-03-17  |  4KB  |  110 lines

  1. // Copyright 1994 A.D. Software. All Rights Reserved
  2.  
  3. // OOFTEST1
  4.  
  5. // This sample tests the database backend by creating a single table
  6. // and storing and retrieving indexed data.
  7.  
  8. // Simple stream I/O is used to interact with the user.
  9.  
  10. // NOTE the odd sizes in the fields below are to help trap alignment issues
  11.  
  12. #include "oofile.hpp"
  13. #include "ooftst01.inc"
  14.  
  15. int main()
  16. {
  17.     cout << "OOFILE Validation Suite - Test 1\n"
  18.          << "Simple test to store some data and retrieve it\n"
  19.          << "in a single-table database, with no relations\n";
  20.     
  21.     dbConnect_ctree    theDB;
  22.     dbPeople     People;
  23.  
  24.     theDB.useSeparateFiles();  // note the blank connection names!
  25. // this test creates People.dat, People.idx & Blobs
  26.  
  27. #ifdef _Macintosh
  28. // this feature only on the Mac at present
  29.     #define kExistsName  ":test01:People.dat"
  30.     #define kDatabaseName "test01"
  31. #else
  32.     #define kExistsName "People.dat"
  33.     #define kDatabaseName ""
  34. #endif    
  35.  
  36.     if (dbConnect::fileExists(kExistsName)) {
  37.         theDB.openConnection(kDatabaseName);
  38.         People.deleteAll();
  39.     }
  40.     else {
  41.         theDB.newConnection(kDatabaseName);
  42.     }
  43.     People.AddTestData();
  44.     People.setSortOrder(People.LastName);
  45.     cout << "Listing records\n" << People;
  46. // NOTE we dumped all the fields of People this time, from now on we will
  47. // use dbViews to restrict the fields listed
  48.  
  49.     dbView justNames(People);
  50.     justNames << People.LastName << People.OtherNames;
  51.     
  52.     People.setSortOrder(People.OtherNames);
  53.     cout << "Listing records in OtherNames order\n" << justNames << endl;
  54.  
  55.     cout << "Now retrieving by index\n";
  56.     cout << "Retrieving Taylor: " << People[People.LastName=="Taylor"].LastName << endl;
  57.  
  58.     cout << "Retrieving Smith by Salary: ";
  59.     People[People.Salary==0];
  60.     cout << People.LastName << endl << endl;
  61.  
  62.     People.search(People.LastName=="Dent");
  63.     cout << "Listing two Dent records: " << endl << justNames << endl;
  64.  
  65.     cout << endl << "now producing a subset of Taylor & Dent" << endl;
  66.     dbPeople entrepeneurs = People;    
  67.     entrepeneurs.search(entrepeneurs.LastName=="Taylor");
  68.     entrepeneurs += People;
  69.     cout << (dbView(entrepeneurs) << entrepeneurs.LastName << entrepeneurs.OtherNames) << endl;
  70.  
  71.     dbPeople savedEnts = entrepeneurs;
  72.     cout << "Just cloned a selection of " << savedEnts.count() << " records" << endl;
  73.     
  74.     cout << endl << "now reducing original via Intersection to Dent" << endl;
  75.     entrepeneurs &= People;      // Dent, Taylor & Dent
  76.     cout << entrepeneurs;
  77.  
  78.     cout << endl << "now Inverting to Smith & Taylor" << endl;
  79.     ~entrepeneurs;
  80.     cout << (dbView(entrepeneurs) << entrepeneurs.LastName << entrepeneurs.OtherNames) << endl;   
  81.     // Dent inverted
  82.  
  83.     cout << "Show the cloned selection is still Dent & Taylor" << endl
  84.     << (dbView(savedEnts) << savedEnts.LastName << savedEnts.OtherNames) << endl;
  85.     
  86.     cout << endl << "now reducing via Difference to Dent" << endl;
  87.     savedEnts -= entrepeneurs;  // Dent, Taylor - Smith, Taylor
  88.     cout << (dbView(savedEnts) << savedEnts.LastName << savedEnts.OtherNames) << endl;
  89.     
  90. // demonstrate going to a specific relative record in a selection
  91. // then going to the previous record, as would be common in a GUI
  92. // by double-clicking a line in a browser, then pressing Prev Record
  93.     People.selectAll();
  94.     People.setSortOrder(People.LastName);
  95.     cout << "Retrieving Taylor by relative record number: ";
  96.     People.gotoRecord(3);
  97.  
  98.     cout << People.LastName << endl << endl;
  99.     cout << "Retrieving previous record Smith: ";
  100.     People.prev();
  101.     cout << People.LastName << endl << endl;
  102.     
  103.     cout << "Now finding people who've been paid in the last 2 weeks " << endl;
  104.     People.search(People.LastPaid > dbDate::currentDate()-14);
  105.     cout << (dbView(People) << People.LastName << People.OtherNames << People.LastPaid) << endl;
  106.  
  107.     cout << "Test Completed" << endl;
  108.     
  109.     return EXIT_SUCCESS;